﻿using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using VOL.Entity.DomainModels;
using VOL.Entity.SystemModels;
using VOL.Team.Comm;
using VOL.Team.TeamIServices.Device_Management;

namespace VOL.WebApi.Controllers.Team.wyx
{
    /// <summary>
    /// 设备管理控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class EquipmentManagementController : ControllerBase
    {
        /// <summary>
        /// 设备管理服务接口
        /// </summary>
        private readonly IEquipmentLedgerService _service;
        /// <summary>
        /// 设备管理控制器构造函数
        /// </summary>
        /// <param name="service">设备管理服务接口</param>
        public EquipmentManagementController(IEquipmentLedgerService service)
        {
            _service = service;
        }

        /// <summary>
        /// 查询设备台账列表，支持分页和条件查询
        /// </summary>
        /// <param name="query">请求</param>
        /// <returns>返回任务</returns>
        [HttpGet("EquipmentLedgerQuery")]//http://localhost:9991/api/EquipmentManagement/EquipmentLedgerQuery/EquipmentLedgerQuery    ?PageSize=10
        public APIPaging<EquipmentLedger> EquipmentLedgerQuery(Query_Condition_EL query)
        {
            // 调用服务查询设备台账列表
            return _service.EquipmentLedgerQuery(query);
        }

        /// <summary>
        /// 添加设备
        /// </summary>
        /// <param name="equipment">请求</param>
        /// <returns>返回结果</returns>
        [HttpPost("AddDevice")]
        public async Task<IActionResult> AddDevice([FromBody] EquipmentLedger equipment)
        {
            var result = await _service.AddEquipmentAsync(equipment);
            return Ok(new { code = 200, message = "添加成功", data = result });
        }

        /// <summary>
        /// 更新设备
        /// </summary>
        /// <param name="equipment">请求</param>
        /// <returns>返回结果</returns>
        [HttpPost("UpdateDevice")]
        public async Task<IActionResult> UpdateDevice([FromBody] EquipmentLedger equipment)
        {
            var result = await _service.UpdateEquipmentAsync(equipment);
            return Ok(new { code = 300, message = "更新成功", data = result });
        }

        /// <summary>
        /// 删除单条
        /// </summary>
        /// <param name="equipment">请求</param>
        /// <returns>返回结果</returns>
        [HttpPost("DeleteDevice")]
        public async Task<IActionResult> DeleteDevice(int deviceId)
        {
            var result = await _service.DeleteEquipmentAsync(deviceId);
            return Ok(new { code = 400, message = "删除成功", data = result });
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="equipment">请求</param>
        /// <returns>返回结果</returns>
        [HttpPost("BatchDeleteDevice")]
        public async Task<IActionResult> BatchDeleteDevice([FromBody] List<int> deviceIds)
        {
            var result = await _service.DeleteEquipmentsAsync(deviceIds);
            return Ok(new { code = 400, message = "批量删除成功", data = result });
        }

        /// <summary>
        /// 获取设备列表（带筛选）
        /// </summary>
        /// <param name="name"></param>
        /// <param name="code"></param>
        /// <param name="status"></param>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet("DeviceList")]
        public async Task<IActionResult> GetDeviceList(
            string name = null,
            string code = null,
            int? status = null,
            int page = 1,
            int pageSize = 10
        )
        {
            var list = await _service.GetFilteredEquipmentsAsync(name, code, status);
            return Ok(new
            {
                code = 100,
                message = "查询成功",
                data = new
                {
                    list,
                    total = list.Count,
                    page,
                    pageSize
                }
            });
        }

        // 获取列配置
        [HttpGet("GetColumns")]
        public async Task<IActionResult> GetColumns()
        {
            var columns = await _service.GetEquipmentColumnsAsync();
            return Ok(columns);
        }

        // 更新列可见性
        [HttpPost("UpdateColumnVisibility")]
        public async Task<IActionResult> UpdateColumnVisibility(
            string columnName,
            bool isVisible
        )
        {
            await _service.UpdateColumnVisibilityAsync(columnName, isVisible);
            return Ok(new { code = 200, message = "配置更新成功" });
        }
    }
}
